package com.ruoyi.dmsIms.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.dmsIms.domain.vo.DmsInter;
import com.ruoyi.dmsIms.domain.vo.SimpleInterList;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dmsIms.domain.ProInterface;
import com.ruoyi.dmsIms.service.IProInterfaceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目接口Controller
 * 
 * @author ZhangSiRui
 * @date 2021-12-09
 */
@RestController
@RequestMapping("/dmsIms/interface")
public class ProInterfaceController extends BaseController
{
    @Autowired
    private IProInterfaceService proInterfaceService;

    /**
     * 查询项目接口列表
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:interface:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProInterface proInterface)
    {
        startPage();
        List<ProInterface> list = proInterfaceService.selectProInterfaceList(proInterface);
        return getDataTable(list);
    }

    /**
     * 接口简易列表，用于展示接口简易信息
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:execute:list')")
    @GetMapping("/splist/{id}")
    public TableDataInfo simplelist(@PathVariable("id") String id)
    {
        List<SimpleInterList> list = proInterfaceService.selectSimpleInterfaceList(id);
        return getDataTable(list);
    }

    /**
     * 接口详细列表，用于展示接口执行中的详细数据
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:execute:list')")
    @GetMapping("/delist")
    public AjaxResult detaillist(Long id)
    {
        return AjaxResult.success(proInterfaceService.selectDetailInter(id));
    }
    /**
     * 导出项目接口列表
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:interface:export')")
    @Log(title = "项目接口", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProInterface proInterface)
    {
        List<ProInterface> list = proInterfaceService.selectProInterfaceList(proInterface);
        ExcelUtil<ProInterface> util = new ExcelUtil<ProInterface>(ProInterface.class);
        util.exportExcel(response, list, "项目接口数据");
    }

    /**
     * 获取项目接口详细信息
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:interface:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(proInterfaceService.selectProInterfaceById(id));
    }

    /**
     * 新增项目接口
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:interface:add')")
    @Log(title = "项目接口", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProInterface proInterface)
    {
        return toAjax(proInterfaceService.insertProInterface(proInterface));
    }

    /**
     * 修改项目接口
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:interface:edit')")
    @Log(title = "项目接口", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProInterface proInterface)
    {
        return toAjax(proInterfaceService.updateProInterface(proInterface));
    }

    /**
     * 删除项目接口
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:interface:remove')")
    @Log(title = "项目接口", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(proInterfaceService.deleteProInterfaceByIds(ids));
    }

    /**
     * 执行接口
     * @param id
     * @return
     */
    @GetMapping("/execute/{id}")
    public AjaxResult executeInterface(@PathVariable("id") Long id)
    {
        Map<String, Object> res = proInterfaceService.executeInterPro(id);
        return AjaxResult.success(res);
    }

    /**
     * 测试接口
     * @param
     * @return
     */
    @PostMapping("/test")
    public AjaxResult mytest()
    {
        return AjaxResult.success(proInterfaceService.selectDetailInter(1L));
    }

    /**
     * 执行接口
     * @param
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('dmsIms:execute:list')")
    @PostMapping("/dmsExecute")
    public AjaxResult dmsExecute(@RequestBody DmsInter dmsInter)
    {
        // method 1:get 2:post
        /**
         * 1、分解header参数
         * 2、判断body参数类型
         * 3、分解body参数
         * 4、发送请求
         */
        Map<String, Object> res = proInterfaceService.executeInter(dmsInter);
        return AjaxResult.success(res);
    }
}
